package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.object.pojo.VipCard;
import com.tbit.uqbike.service.baseAbstract.SynchronousService;
import com.tbit.uqbike.webmanager.dao.core.VipCardDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 会员卡配置
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2025-2-21
 */
@Service("membershipCardConfigService")
@Slf4j
public class MembershipCardConfigServiceImpl extends SynchronousService {

    @Resource
    private VipCardDao vipCardDao;


    @Override
    protected List<?> queryConfig(Integer accountId) {
        List<VipCard> vipCards = vipCardDao.selectByAccountId(accountId);
        if (CollectionUtil.isNotEmpty(vipCards)) {
            return vipCards;
        } else {
            return Collections.emptyList();
        }
    }

    @Override
    protected void deleteConfig(Integer accountId) {
        List<VipCard> vipCards = vipCardDao.selectByAccountId(accountId);
        if (CollectionUtil.isNotEmpty(vipCards)) {
            log.info("同步配置删除会员卡配置:{}", JSON.toJSONString(vipCards));
            VipCard vipCard = new VipCard();
            vipCard.setAccountId(accountId);
            vipCardDao.del(vipCard);
        }
    }


    @Override
    protected void insertConfig(List<?> objects, Integer accountId) {
        if (CollectionUtil.isEmpty(objects)) {
            return;
        }
        List<VipCard> vipCards = (List<VipCard>) objects;
        vipCards.forEach(vipCard -> vipCard.setAccountId(accountId));
        vipCardDao.batchAdd(vipCards);
    }
}
